import pymysql
from pymysql.cursors import DictCursor


class MySql:

    def __init__(self,
                 host,
                 port,
                 user,
                 password,
                 charset,
                 database,
                 cursorclass=DictCursor):
        self.connect = pymysql.connect(host=host, port=port, user=user,
                                       password=password, charset=charset,
                                       database=database, cursorclass=cursorclass)
        self.cursor = self.connect.cursor()

    def select(self, sql, args=None, one=True):
        self.cursor.execute(sql, args)
        self.connect.commit()
        if one:
            return self.cursor.fetchone()
        else:
            return self.cursor.fetchall()

    def close(self):
        self.connect.close()
        self.cursor.close()